 
 #include "stdafx.h"
 #include "Service.h"
  
  Service::Service()
  {
     scm=OpenSCManager(0,0,SC_MANAGER_ALL_ACCESS);
 }
 
  Service::~Service()
  {
      CloseServiceHandle(scm);
  }
 

 BOOL Service::Create()
   {
       BOOL suc=false;
       SC_HANDLE tmp;
       if(ServiceName && 
 DisplayName && BinaryPathName)
       {   
           tmp=CreateService(scm,ServiceName,
               DisplayName,
               SERVICE_ALL_ACCESS,SERVICE_WIN32_OWN_PROCESS,
               AutoStart?SERVICE_AUTO_START:SERVICE_DEMAND_START,
               SERVICE_ERROR_NORMAL,
               BinaryPathName,
               0,0,0,0,0);
           if(tmp)
           {
               suc=true;
           }
           CloseServiceHandle(tmp);
       }
       return suc;
   }
   
   BOOL Service::Delete()
   {
       BOOL suc=false;
       SC_HANDLE tmp;
       if(ServiceName)
       {   
           tmp=OpenService(scm,ServiceName,
               SERVICE_ALL_ACCESS);
           if(tmp)
           {
               if(DeleteService(tmp))
               {
                   suc=true;
               }
               
           }
           CloseServiceHandle(tmp);
       }
       return suc;
   
   }
   
   BOOL Service::Start()
   {
       BOOL suc=false;
       SC_HANDLE tmp;
       if(ServiceName)
       {   
           tmp=OpenService(scm,ServiceName,
               SERVICE_ALL_ACCESS);
           if(tmp)
           {
               if(StartService(tmp,0,NULL))
               {
                   suc=true;
               }
               
           }
           CloseServiceHandle(tmp);
       }
       return suc;
   }
   
   BOOL Service::Stop()
   {
       BOOL suc=false;
       SC_HANDLE tmp;
       if(ServiceName)
      {   
          tmp=OpenService(scm,ServiceName, SERVICE_ALL_ACCESS);
          if(tmp)
          {
              SERVICE_STATUS SERVICE_STATUS;
              
              if(ControlService(tmp,SERVICE_CONTROL_STOP, &SERVICE_STATUS))
              {
                  suc=true;
              }
              
          }
          CloseServiceHandle(tmp);
      }
      return suc;
  
  }
  
  BOOL Service::Pause()
  {
      BOOL suc=false;
      SC_HANDLE tmp;
      if(ServiceName)
      {   
          tmp=OpenService(scm,ServiceName,
              SERVICE_ALL_ACCESS);
          if(tmp)
          {
              SERVICE_STATUS SERVICE_STATUS;
              
              if(ControlService(tmp,SERVICE_CONTROL_PAUSE, &SERVICE_STATUS))
              {
                  suc=true;
              }
              
          }
          CloseServiceHandle(tmp);
      }
      return suc;
  
  }
  
  BOOL Service::Continue()
  {
      BOOL suc=false;
      SC_HANDLE tmp;
      if(ServiceName)
      {   
          tmp=OpenService(scm,ServiceName, SERVICE_ALL_ACCESS);
          if(tmp)
          {
              SERVICE_STATUS m_SERVICE_STATUS;
              
              if(ControlService(tmp, SERVICE_CONTROL_CONTINUE, &m_SERVICE_STATUS))
              {
                  suc=true;
              }
              
          }
          CloseServiceHandle(tmp);
      }
      return suc;
  }
  
  void Service::SetServiceName(LPCTSTR str)
  {
      ServiceName = str;
  }
  
  void Service::SetServiceDisplayName(LPCTSTR str)
  {
      DisplayName = str;
  }
  
  void Service::SetServicePath(LPCTSTR str)
  {
      BinaryPathName = str;
  }
  
  void Service::SetAutoStart(BOOL b)
  {
      AutoStart = b;
  }